using System;
using System.Collections.Generic;
using System.Linq;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Wordprocessing;

namespace converter.template.item
{
    internal class Table : TemplateItem
    {
        public Table(string name)
        {
            dataSetName = name;
        }

        public string DataSetName
        {
            get
            {
                return dataSetName;
            }

            set
            {
                dataSetName = value;
            }
        }

        /// <summary>
        /// Creates the Table element.
        /// </summary>
        /// <param name="parent">The parent element -- Body.</param>
        public override void Build(OpenXmlCompositeElement parent)
        {
            DocumentFormat.OpenXml.Wordprocessing.Table table = new DocumentFormat.OpenXml.Wordprocessing.Table();

            TableProperties tableProperties = new TableProperties();
            TableStyle tableStyle = new TableStyle() { Val = "TableGrid" };
            TableWidth tableWidth = new TableWidth() { Width = "0", Type = TableWidthUnitValues.Auto };
            TableLook tableLook = new TableLook() { Val = "04A0" };

            // Table borders
            TableBorders tableBorders = new TableBorders();
            TopBorder topBorder = new TopBorder() { Val = GetStyle().Borders.TopBorder.ToBorderValues(), Color = "auto", Size = (UInt32Value)0U, Space = (UInt32Value)0U };
            LeftBorder leftBorder = new LeftBorder() { Val = GetStyle().Borders.LeftBorder.ToBorderValues(), Color = "auto", Size = (UInt32Value)0U, Space = (UInt32Value)0U };
            BottomBorder bottomBorder = new BottomBorder() { Val = GetStyle().Borders.BottomBorder.ToBorderValues(), Color = "auto", Size = (UInt32Value)0U, Space = (UInt32Value)0U };
            RightBorder rightBorder = new RightBorder() { Val = GetStyle().Borders.RightBorder.ToBorderValues(), Color = "auto", Size = (UInt32Value)0U, Space = (UInt32Value)0U };
            tableBorders.Append(topBorder);
            tableBorders.Append(leftBorder);
            tableBorders.Append(bottomBorder);
            tableBorders.Append(rightBorder);

            tableProperties.Append(tableStyle);
            tableProperties.Append(tableWidth);
            tableProperties.Append(tableBorders);
            tableProperties.Append(tableLook);

            table.Append(tableProperties);

            // Build the table rows.
            foreach (TemplateItem item in Items)
                item.Build(table);

            parent.AppendChild(table);
        }

        private string dataSetName;
    }
}